package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.CommonDict;
import dicts.PaymentDict;
import dicts.RealmDict;
import entities.TBalance;
import entities.TMb;
import entities.TMerchant;
import entities.TSettle;
import interfaces.IBalanceService;
import interfaces.IMbService;
import interfaces.ISettleService;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class MbAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IBalanceService balanceService;
	private TBalance balanceBean;
	private List<TBalance> balanceList;
	private String balNo;
	private Double balSm;
	private Double balMs;
	private Double balBm;
	private Double balMb;
	private Double balTotal;

	private ISettleService settleService;
	private TSettle settleBean;
	private List<TSettle> settleList;
	private String steNo;
	private Double steSm;
	private Double steMs;
	private Double steBm;
	private Double steMb;
	private Double steTotal;
	
	private IMbService mbService;
	private TMb mbBean;
	private List<TMb> mbList;
	private String mbNo;
	private String mbNos;
	private String merNo;
	private String mbType;
	private String mbBank;
	private String mbBranch;
	private String mbBanker;
	private String mbAccount;
	private Double mbAmount;
	private String mbBankz;
	private String mbBranchz;
	private String mbBankerz;
	private String mbAccountz;
	private String mbState;
	private Date mbDate;
	private Date mbDateTop;
	private Date mbDateEnd;
	private String mbVstate;
	private Date mbVdate;
	private Date mbVdateTop;
	private Date mbVdateEnd;
	private String mbRemark;
	private Long mbBetween;
	private Double mbTotal;

	// 展示对象
	public String show() throws Exception {
		if (!mbService.exist(mbNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			mbBean = mbService.fetch(mbNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String editsettle() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merNo", ((TMerchant) ActionContext.getContext().getSession().get("merchant")).getMerNo());
		paramMap.put("mbState", "1");
		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, 1));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();
		
		if(mbList.size()>0){
			mbBean=mbList.get(0);
			mbBetween=DateUtil.distanceDays(mbBean.getMbDate(), new Date());
		}else{
			mbBetween=new Long(999);
		}
		
		balanceBean = balanceService.fetch(((TMerchant) ActionContext.getContext().getSession().get("merchant")).getMerNo());
		settleBean = settleService.fetch(((TMerchant) ActionContext.getContext().getSession().get("merchant")).getMerNo());
		return SUCCESS;
	}

	// 持久化对象
	public String savesettle() throws Exception {
		JSONObject resultMap = new JSONObject();
		Map<String, String> paramsMap = new HashMap<String, String>();

		try{
			paramsMap.put("merNo", ((TMerchant)ActionContext.getContext().getSession().get("merchant")).getMerNo());
			paramsMap.put("mbAmount", NumberUtil.transfer(mbAmount));
			resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Settle", paramsMap));
		}catch(Exception e){
			addActionError("网络连接异常,请稍后再试.");
			return INFO;
		}
		
		if(!resultMap.getString("code").equals("0000")){
			addActionError("操作失败,错误原因 : ["+resultMap.getString("code")+"]    "+resultMap.getString("message")+"");
			return INFO;
		}
		return SUCCESS;
	}

	public String listsettle() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", ((TMerchant)ActionContext.getContext().getSession().get("merchant")).getMerNo());
		paramMap.put("mbType", PaymentDict.MB_SETTLE);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		
		querySet.setPolymerizeInfo("mbAmount");
		mbTotal = mbService.summary(querySet);
		return SUCCESS;
	}
	
	public String listpenalty() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", ((TMerchant)ActionContext.getContext().getSession().get("merchant")).getMerNo());
		paramMap.put("mbType", PaymentDict.MB_PENALTY);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		
		querySet.setPolymerizeInfo("mbAmount");
		mbTotal = mbService.summary(querySet);
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String exportsettle() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", ((TMerchant)ActionContext.getContext().getSession().get("merchant")).getMerNo());
		paramMap.put("mbType",  PaymentDict.MB_SETTLE);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("商户编号");
		fieldHead.add("结算类型");
		fieldHead.add("结算银行名称");
		fieldHead.add("支行");
		fieldHead.add("结算银行用户名");
		fieldHead.add("结算银行帐号");
		fieldHead.add("金额");
		fieldHead.add("汇款银行");
		fieldHead.add("汇款支行");
		fieldHead.add("汇款账户");
		fieldHead.add("汇款账户名");
		fieldHead.add("提现状态");
		fieldHead.add("发生日期日期");
		fieldHead.add("发生日期时间");
		fieldHead.add("审核状态");
		fieldHead.add("审核日期日期");
		fieldHead.add("审核日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TMb mbBean : mbList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(mbBean.getMbNo());
			fieldItem.add(mbBean.getMerNo());
			fieldItem.add(mbBean.getMbType());
			fieldItem.add(mbBean.getMbBank());
			fieldItem.add(mbBean.getMbBranch());
			fieldItem.add(mbBean.getMbBanker());
			fieldItem.add(mbBean.getMbAccount());
			fieldItem.add(mbBean.getMbAmount().toString());
			fieldItem.add(mbBean.getMbBankz());
			fieldItem.add(mbBean.getMbBranchz());
			fieldItem.add(mbBean.getMbBankerz());
			fieldItem.add(mbBean.getMbAccountz());
			fieldItem.add((mbBean.getMbState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(mbBean.getMbDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(mbBean.getMbDate(), DateUtil.TIME));
			fieldItem.add((mbBean.getMbVstate().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(mbBean.getMbVdate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(mbBean.getMbVdate(), DateUtil.TIME));
			fieldItem.add(mbBean.getMbRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IMbService getMbService() {
		return mbService;
	}

	public void setMbService(IMbService mbService) {
		this.mbService = mbService;
	}

	public TMb getMbBean() {
		return mbBean;
	}

	public void setMbBean(TMb mbBean) {
		this.mbBean = mbBean;
	}

	public List<TMb> getMbList() {
		return mbList;
	}

	public void setMbList(List<TMb> mbList) {
		this.mbList = mbList;
	}

	public String getMbNo() {
		return mbNo;
	}

	public void setMbNo(String mbNo) {
		this.mbNo = mbNo;
	}

	public String getMbNos() {
		return mbNos;
	}

	public void setMbNos(String mbNos) {
		this.mbNos = mbNos;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getMbType() {
		return mbType;
	}

	public void setMbType(String mbType) {
		this.mbType = mbType;
	}

	public String getMbBank() {
		return mbBank;
	}

	public void setMbBank(String mbBank) {
		this.mbBank = mbBank;
	}

	public String getMbBranch() {
		return mbBranch;
	}

	public void setMbBranch(String mbBranch) {
		this.mbBranch = mbBranch;
	}

	public String getMbBanker() {
		return mbBanker;
	}

	public void setMbBanker(String mbBanker) {
		this.mbBanker = mbBanker;
	}

	public String getMbAccount() {
		return mbAccount;
	}

	public void setMbAccount(String mbAccount) {
		this.mbAccount = mbAccount;
	}

	public Double getMbAmount() {
		return mbAmount;
	}

	public void setMbAmount(Double mbAmount) {
		this.mbAmount = mbAmount;
	}

	public String getMbBankz() {
		return mbBankz;
	}

	public void setMbBankz(String mbBankz) {
		this.mbBankz = mbBankz;
	}

	public String getMbBranchz() {
		return mbBranchz;
	}

	public void setMbBranchz(String mbBranchz) {
		this.mbBranchz = mbBranchz;
	}

	public String getMbBankerz() {
		return mbBankerz;
	}

	public void setMbBankerz(String mbBankerz) {
		this.mbBankerz = mbBankerz;
	}

	public String getMbAccountz() {
		return mbAccountz;
	}

	public void setMbAccountz(String mbAccountz) {
		this.mbAccountz = mbAccountz;
	}

	public String getMbState() {
		return mbState;
	}

	public void setMbState(String mbState) {
		this.mbState = mbState;
	}

	public Date getMbDate() {
		return mbDate;
	}

	public void setMbDate(Date mbDate) {
		this.mbDate = mbDate;
	}

	public Date getMbDateTop() {
		return mbDateTop;
	}

	public void setMbDateTop(Date mbDateTop) {
		this.mbDateTop = mbDateTop;
	}

	public Date getMbDateEnd() {
		return mbDateEnd;
	}

	public void setMbDateEnd(Date mbDateEnd) {
		this.mbDateEnd = mbDateEnd;
	}

	public String getMbVstate() {
		return mbVstate;
	}

	public void setMbVstate(String mbVstate) {
		this.mbVstate = mbVstate;
	}

	public Date getMbVdate() {
		return mbVdate;
	}

	public void setMbVdate(Date mbVdate) {
		this.mbVdate = mbVdate;
	}

	public Date getMbVdateTop() {
		return mbVdateTop;
	}

	public void setMbVdateTop(Date mbVdateTop) {
		this.mbVdateTop = mbVdateTop;
	}

	public Date getMbVdateEnd() {
		return mbVdateEnd;
	}

	public void setMbVdateEnd(Date mbVdateEnd) {
		this.mbVdateEnd = mbVdateEnd;
	}

	public String getMbRemark() {
		return mbRemark;
	}

	public void setMbRemark(String mbRemark) {
		this.mbRemark = mbRemark;
	}

	public IBalanceService getBalanceService() {
		return balanceService;
	}

	public void setBalanceService(IBalanceService balanceService) {
		this.balanceService = balanceService;
	}

	public TBalance getBalanceBean() {
		return balanceBean;
	}

	public void setBalanceBean(TBalance balanceBean) {
		this.balanceBean = balanceBean;
	}

	public List<TBalance> getBalanceList() {
		return balanceList;
	}

	public void setBalanceList(List<TBalance> balanceList) {
		this.balanceList = balanceList;
	}

	public String getBalNo() {
		return balNo;
	}

	public void setBalNo(String balNo) {
		this.balNo = balNo;
	}

	public Double getBalSm() {
		return balSm;
	}

	public void setBalSm(Double balSm) {
		this.balSm = balSm;
	}

	public Double getBalMs() {
		return balMs;
	}

	public void setBalMs(Double balMs) {
		this.balMs = balMs;
	}

	public Double getBalBm() {
		return balBm;
	}

	public void setBalBm(Double balBm) {
		this.balBm = balBm;
	}

	public Double getBalMb() {
		return balMb;
	}

	public void setBalMb(Double balMb) {
		this.balMb = balMb;
	}

	public Double getBalTotal() {
		return balTotal;
	}

	public void setBalTotal(Double balTotal) {
		this.balTotal = balTotal;
	}

	public ISettleService getSettleService() {
		return settleService;
	}

	public void setSettleService(ISettleService settleService) {
		this.settleService = settleService;
	}

	public TSettle getSettleBean() {
		return settleBean;
	}

	public void setSettleBean(TSettle settleBean) {
		this.settleBean = settleBean;
	}

	public List<TSettle> getSettleList() {
		return settleList;
	}

	public void setSettleList(List<TSettle> settleList) {
		this.settleList = settleList;
	}

	public String getSteNo() {
		return steNo;
	}

	public void setSteNo(String steNo) {
		this.steNo = steNo;
	}

	public Double getSteSm() {
		return steSm;
	}

	public void setSteSm(Double steSm) {
		this.steSm = steSm;
	}

	public Double getSteMs() {
		return steMs;
	}

	public void setSteMs(Double steMs) {
		this.steMs = steMs;
	}

	public Double getSteBm() {
		return steBm;
	}

	public void setSteBm(Double steBm) {
		this.steBm = steBm;
	}

	public Double getSteMb() {
		return steMb;
	}

	public void setSteMb(Double steMb) {
		this.steMb = steMb;
	}

	public Double getSteTotal() {
		return steTotal;
	}

	public void setSteTotal(Double steTotal) {
		this.steTotal = steTotal;
	}

	public Long getMbBetween() {
		return mbBetween;
	}

	public void setMbBetween(Long mbBetween) {
		this.mbBetween = mbBetween;
	}

	public Double getMbTotal() {
		return mbTotal;
	}

	public void setMbTotal(Double mbTotal) {
		this.mbTotal = mbTotal;
	}

	
}
